package com.senlan.water.dao.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.senlan.water.dao.domain.Role;
import com.senlan.water.dao.domain.User;
import com.senlan.water.dao.mapper.UserMapper;
import com.senlan.water.dao.service.RoleMenuService;
import com.senlan.water.dao.service.RoleService;
import com.senlan.water.dao.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
* @author 10499
* @description 针对表【user(用户信息表)】的数据库操作Service实现
* @createDate 2024-12-03 10:30:09
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService {

    @Resource
    private RoleService roleService;

    @Resource
    private RoleMenuService roleMenuService;

    @Override
    public User getUserByName(String name) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,name);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<Role> getRoleList(Long userId) {
        if (userId != null){
            User user = getById(userId);
            if (user != null){
                List<String> roleIdList = Arrays.asList(user.getRoleIds().split(","));
                return roleService.listByIds(roleIdList);
            }
        }
        return Collections.emptyList();
    }

    @Override
    public List<String> getPermissionList(Long userId) {
        List<String> permissionList = new ArrayList<>();
        List<Role> roleList = getRoleList(userId);
        for (Role role:roleList){
            //获取角色的所有菜单权限
            List<String> rolePermissionList = roleMenuService.getRolePermissionList(role.getId());
            if (CollectionUtil.isNotEmpty(rolePermissionList)){
                permissionList.addAll(rolePermissionList);
            }
        }
        return permissionList;
    }
}




